import java.awt.Graphics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Programm extends JPanel{
	
	private static final long serialVersionUID = 1L;
	int x, y;
	boolean windowResized = true;
	
	public Programm() throws IOException {
		addMouseListener(new MouseAdapter() { 
			public void mousePressed(MouseEvent me) {
				try {
					drawingAreaMousePressed(me);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public void createFrame() throws IOException {
		JFrame frame = new JFrame();
		frame.setTitle("eBoard");
		frame.getContentPane().add(new Programm());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(200,200);
		frame.setVisible(true);
		addComponentListener (new ComponentAdapter() {
			public void componentResized(ComponentEvent evt) {
				drawingAreaWindowResized();
			}
			public void componentHidden(ComponentEvent evt) {
				drawingAreaWindowResized();
			}
		});
		WindowListener listener = new WindowAdapter() {
			public void windowDeiconified(WindowEvent arg0) {
				drawingAreaWindowResized();
			}
		};
		frame.addWindowListener (listener);
	}
	
	public void writeFile(int x, int y) throws IOException {
		String dateiName = "Test.txt";
		FileOutputStream schreibeStrom = new FileOutputStream(dateiName,true);
		DataOutputStream dos = new DataOutputStream(schreibeStrom);
		dos.writeInt(x);
		dos.writeInt(y);
		dos.close();
		schreibeStrom.close();
	}
	
	public void drawingAreaWindowResized() {
		windowResized = true;
	}
	
	public void drawingAreaWindowShown() {
		System.out.println("Fenster wird angezeigt.");
	}
	
	private void drawingAreaMousePressed(MouseEvent evt) throws IOException {
		x = evt.getX();
		y = evt.getY();
		writeFile(x,y);
		System.out.println("Maus gedrueckt");
		repaint();
	}
	
	public void paint(Graphics g) {
		boolean redraw = false;
		if (windowResized == true) {
			redraw = true;
		}
		System.out.println("paint() augerufen, windowResized: " + windowResized);
		if (redraw == true) {
			String dateiName = "Test.txt";
			FileInputStream schreibeStrom = null;
			try {
				schreibeStrom = new FileInputStream(dateiName);
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			}
			DataInputStream dos = new DataInputStream(schreibeStrom);
			try {
				while(true) {
					x = dos.readInt();
					y = dos.readInt();
					g.fillOval(x-8, y-8, 16, 16);
				}
			} catch (EOFException eofe) {
				try {
					System.out.println("test1");
					dos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				try {
					schreibeStrom.close();
					System.out.println("Datei gelesen!");
					System.out.println("windowResized = false");
				} catch (IOException e) {
					e.printStackTrace();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		else {
			g.fillOval(x-8, y-8, 16, 16);
			System.out.println("Datei nicht gelesen.");
		}
	}
}